package com.kantboot.user.account.web.controller;

import com.kantboot.global.rest.result.CommonSuccessStateCodeAndMsg;
import com.kantboot.user.account.domain.dto.UserAccountSendVerificationCodeDTO;
import com.kantboot.user.account.domain.vo.LoginVO;
import com.kantboot.user.account.service.IUserAccountLoginService;
import com.kantboot.util.common.result.RestResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户账号登录控制器
 */
@RestController
@RequestMapping("/user-account-web/userAccountLogin")
public class UserAccountLoginController {

    @Resource
    private IUserAccountLoginService service;

    /**
     * 发送登录验证码
     * @param sendVerificationCodeDTO 发送验证码的DTO
     * @return void
     */
    @RequestMapping("/sendLoginVerificationCode")
    public RestResult<Void> sendLoginVerificationCode(@RequestBody UserAccountSendVerificationCodeDTO sendVerificationCodeDTO) {
        service.sendLoginVerificationCode(sendVerificationCodeDTO);
        return RestResult.success(null,"sendLoginVerificationCodeSuccess","发送登录验证码成功");
    }

    /**
     * 根据用户名和密码登录
     * 通常不开放
     * @param username 用户名
     * @param password 密码
     */
    @RequestMapping("/loginByUsernameAndPassword")
    public RestResult<LoginVO> loginByUsernameAndPassword(
            @RequestParam("username") String username,
            @RequestParam("password") String password) {
        return RestResult.success(service.loginByUsernameAndPassword(username, password),
                CommonSuccessStateCodeAndMsg.LOGIN_SUCCESS);
    }

    /**
     * 根据手机、验证码登录
     * @param phoneAreaCode 手机区号
     * @param phone 手机号
     * @param verificationCode 验证码
     * @return void
     */
    @RequestMapping("/loginByPhoneAndVerificationCode")
    public RestResult<LoginVO> loginByPhoneAndVerificationCode(
            @RequestParam("phoneAreaCode") String phoneAreaCode,
            @RequestParam("phone") String phone,
            @RequestParam("verificationCode") String verificationCode) {
        return RestResult.success(service.loginByPhoneAndVerificationCode(phoneAreaCode, phone, verificationCode),
                CommonSuccessStateCodeAndMsg.LOGIN_SUCCESS);
    }

    /**
     * 根据邮箱、验证码登录
     * @param email 邮箱
     * @param verificationCode 验证码
     * @return void
     */
    @RequestMapping("/loginByEmailAndVerificationCode")
    public RestResult<LoginVO> loginByEmailAndVerificationCode(
            @RequestParam("email") String email,
            @RequestParam("verificationCode") String verificationCode) {
        return RestResult.success(service.loginByEmailAndVerificationCode(email, verificationCode),
                CommonSuccessStateCodeAndMsg.LOGIN_SUCCESS);
    }

    /**
     * 根据手机号、密码登录
     * @param phoneAreaCode 手机区号
     * @param phone 手机号
     * @param password 密码
     * @return void
     */
    @RequestMapping("/loginByPhoneAndPassword")
    public RestResult<LoginVO> loginByPhoneAndPassword(
            @RequestParam("phoneAreaCode") String phoneAreaCode,
            @RequestParam("phone") String phone,
            @RequestParam("password") String password) {
        return RestResult.success(service.loginByPhoneAndPassword(phoneAreaCode, phone, password),
                CommonSuccessStateCodeAndMsg.LOGIN_SUCCESS);
    }

    /**
     * 根据邮箱、密码登录
     * @param email 邮箱
     * @param password 密码
     * @return void
     */
    @RequestMapping("/loginByEmailAndPassword")
    public RestResult<LoginVO> loginByEmailAndPassword(
            @RequestParam("email") String email,
            @RequestParam("password") String password) {
        return RestResult.success(service.loginByEmailAndPassword(email, password),
                CommonSuccessStateCodeAndMsg.LOGIN_SUCCESS);
    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public RestResult<Void> logout() {
        service.logout();
        return RestResult.success(null, CommonSuccessStateCodeAndMsg.LOGOUT_SUCCESS);
    }

}
